/*
 * Copyright (C) 2010-2012 Enrique Lara (enrique.posix2.lara@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
 * limitations under the License.
  */
package trs.wmata.api;

import java.io.Serializable;

import org.xml.sax.Attributes;

public final class RouteDetails extends ApiResponse implements Serializable {
    private static final long serialVersionUID = -6128982119100151635L;
    public static final String RouteDetailsInfo = "RouteDetailsInfo";
    public static final String Direction0 = "Direction0";
    public static final String DirectionNum = "DirectionNum";
    public static final String DirectionText = "DirectionText";
    public static final String Shape = "Shape";
    public static final String ShapePoint = "ShapePoint";
    public static final String Lat = "Lat";
    public static final String Lon = "Lon";
    public static final String SeqNum = "SeqNum";
    public static final String Stops = "Stops";
    public static final String Stop = "Stop";
    public static final String Name = "Name";
    public static final String StopID = "StopID";
    public static final String TripHeadsign = "TripHeadsign";
    public static final String Direction1 = "Direction1";
    public static final String RouteID = "RouteID";
    private Direction currentDirection = null;
    private final Direction direction0 = new Direction();
    private final Direction direction1 = new Direction();
    private String routeName;

    public RouteDetails() {
    }

    @Override
    public void startElement(final String lName, final Attributes atts) {
        /* if (RouteDetailsInfo.equals(lName)) {
        } else */
        if (Direction0.equals(lName)) {
            currentDirection = direction0;
        } else if (Direction1.equals(lName)) {
            currentDirection = direction1;
        } else if (DirectionNum.equals(lName)) {
            doCaptureChars = true;
        } else if (DirectionText.equals(lName)) {
            doCaptureChars = true;

            //} else if (Shape.equals(lName)) {
            //} else if (ShapePoint.equals(lName)) {
            //} else if (Lat.equals(lName)) {
            //} else if (Lon.equals(lName)) {
            //} else if (SeqNum.equals(lName)) {
            //} else if (Stops.equals(lName)) {
            //} else if (Stop.equals(lName)) {
        } else if (Name.equals(lName)) {
            doCaptureChars = true;

            //} else if (StopID.equals(lName)) {
            //} else if (TripHeadsign.equals(lName)) {
            //} else if (RouteID.equals(lName)) {
        }
    }

    @Override
    public void endElement(final String lName, final StringBuilder buff) {
        /*if (RouteDetailsInfo.equals(lName)) {
        } else */
        if (Direction0.equals(lName)) {
            currentDirection = null;
        } else if (Direction1.equals(lName)) {
            currentDirection = null;
        } else if (DirectionNum.equals(lName)) {
            if (currentDirection != null) {
                final String str = buff.toString().trim();
                currentDirection.code = str;
            }

            doCaptureChars = false;
        } else if (DirectionText.equals(lName)) {
            if (currentDirection != null) {
                final String str = buff.toString().trim();
                currentDirection.name = str;
            }

            doCaptureChars = false;

            //} else if (Shape.equals(lName)) {
            //} else if (ShapePoint.equals(lName)) {
            //} else if (Lat.equals(lName)) {
            //} else if (Lon.equals(lName)) {
            //} else if (SeqNum.equals(lName)) {
            //} else if (Stops.equals(lName)) {
            //} else if (Stop.equals(lName)) {
        } else if (Name.equals(lName)) {
            final String str = buff.toString().trim();
            routeName = str;
            doCaptureChars = false;

            //} else if (StopID.equals(lName)) {
            //} else if (TripHeadsign.equals(lName)) {
            //} else if (RouteID.equals(lName)) {
        }
    }

    private boolean doCaptureChars = false;

    @Override
    public boolean doCaptureChars() {
        return doCaptureChars;
    }

    static class Direction implements Serializable {
        private static final long serialVersionUID = -684612979619943656L;
        String code = "UNKNOWN";
        String name = "UNKNOWN";
    }

    public String[] getDirectionCodes() {
        return new String[] {
                   direction0.code,
                   direction1.code
               };
    }

    public String[] getDirectionNames() {
        return new String[] {
                   direction0.name,
                   direction1.name
               };
    }

    public String getRouteName() {
        return routeName;
    }
}
